﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Xml.Xsl;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Library.Helpers
{
    public class XlsHelper
    {
        public static string TransformOutput(string xml, string xslPath)
        {
            return TransformOutput(xml, xslPath, null);
        }

        public static string TransformOutput(string xml, string xslPath, XsltArgumentList args)
        {            
            byte[] data = Encoding.UTF8.GetBytes(xml);
            MemoryStream ms = new MemoryStream(data);

            XmlReader input = XmlReader.Create(ms);
            XmlDocument stylesheet = new XmlDocument();
            stylesheet.Load(AppDomain.CurrentDomain.BaseDirectory + xslPath);

            XsltSettings settings = new XsltSettings(true, true);

            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(stylesheet, settings, new XmlUrlResolver());

            MemoryStream buffer = new MemoryStream();
            StreamWriter sw = new StreamWriter(buffer);

            transform.Transform(input, args, sw);

            byte[] chars = buffer.ToArray();
            string output = Encoding.UTF8.GetString(chars).Replace("_x0020_", " ");

            return output;                        
        }
    }
}
